import storage from 'store'
import {
  login,
  getInfo,
  logout,
  logintow
} from '@/api/login'
import {
  ACCESS_TOKEN
} from '@/store/mutation-types'
import {
  welcome
} from '@/utils/util'

import {
  resetRouter
} from '@/router'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {},
    userId:undefined,
    projectInfo: {},
    showBackBtn:undefined,
  },

  mutations: {
    SET_SHOWBACKBTN: (state, bool) => {
      state.showBackBtn=bool
    },
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, {
      name,
      welcome
    }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
      state.userId=info.userId;
    },
    set_userId: (state, userId) => {
      state.userId = userId
    },
    set_projectInfo: (state, projectInfo) => {
      state.projectInfo = projectInfo
    }
  },

  actions: {
    // 登录
    Login({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          storage.set(ACCESS_TOKEN, response.token)
          commit('SET_TOKEN', response.token)
          commit('setpanesUrl', [{
          title: '工作台',
          key: '/dashboard/workplace',
          fullPath: '/dashboard/workplace',
          closable: false,
        }])
          console.log('response',response,'99999999999999')
          resolve()
        })
        .catch(error => {
          reject(error)
        })
      })
    },
    //双因素登录
    logintow({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        logintow(userInfo).then(response => {
          commit('SET_ROLES', [])
          commit('setpanesUrl', [{
          title: '工作台',
          key: '/dashboard/workplace',
          fullPath: '/dashboard/workplace',
          closable: false,
        }])
          storage.set(ACCESS_TOKEN, response.token)
          commit('SET_TOKEN', response.token)
          console.log('response',response,'99999999999999')
          resolve()
        })
        .catch(error => {
          reject(error)
        })
      })
    },
    // 获取用户信息
    GetInfo({
      commit
    }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
          resetRouter()
          const result = response;
          // if (result.menuList && result.menuList.length > 0) {
            commit('SET_ROLES', result.menuList || [{
              path: '*',
              redirect: '/404',
              hidden: true
            }])
            commit('SET_INFO', result)
            commit('SET_NAME', {
              name: result.userName,
              welcome: welcome()
            })
          // } else {
          //   reject(new Error('该用户没有权限!'))
          // }

          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({
      commit,
      state
    }) {
      return new Promise((resolve) => {
        logout().then(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_ROUTERS', [])
          commit('SET_original', [])
          commit('setkeepAliveArr', [])
          commit('setpanes',true)
          commit('projectId','')
          // commit('SET_SHOWBACKBTN','')
          storage.remove(ACCESS_TOKEN)
          resolve()
        }).catch((err) => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_ROUTERS', [])
          commit('SET_original', [])
          commit('keepAliveArr', [])
          commit('panesA',true)
          commit('projectId','')
          // commit('SET_SHOWBACKBTN','')
          storage.remove(ACCESS_TOKEN)
          resolve()
        }).finally(() => {})
      })
    }

  }
}

export default user